package com.wang.helloworld.hellojetty.config;

import java.util.LinkedHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
import org.springframework.security.web.util.matcher.ELRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import com.wang.helloworld.hellojetty.security.AuthenticationProviderImpl;

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
@ComponentScan(basePackages = {
        "com.wang.helloworld.hellojetty.security"
})
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	AuthenticationProviderImpl authenticationProvider;
	
	@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.authenticationProvider(authenticationProvider);
    }

	@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/pub/**", "/signup", "/logout", "/login", "/error/**").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated();

        http.formLogin().loginProcessingUrl("/doLogin").failureUrl("/login?error=true").permitAll()
                .and().logout().logoutUrl("/logout").logoutSuccessUrl("/login?logout=success");

        http.httpBasic().authenticationEntryPoint(getDelegatingAuthenticationEntryPoint());

        http.exceptionHandling().accessDeniedPage("/error/401");
    }

	@Bean
	protected DelegatingAuthenticationEntryPoint getDelegatingAuthenticationEntryPoint()
	{
		LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> linkedHashMap = new LinkedHashMap<RequestMatcher, AuthenticationEntryPoint>();
		
		RequestMatcher requestMatcher = new ELRequestMatcher("hasHeader('user-agent', 'RESTful Client')");
		linkedHashMap.put(requestMatcher, getBasicAuthenticationEntryPoint());
		
		DelegatingAuthenticationEntryPoint delegatingAuthenticationEntryPoint = new DelegatingAuthenticationEntryPoint(linkedHashMap);
		delegatingAuthenticationEntryPoint.setDefaultEntryPoint(getLoginUrlAuthenticationEntryPoint());
		
		return delegatingAuthenticationEntryPoint;
	}

	@Bean
	protected BasicAuthenticationEntryPoint getBasicAuthenticationEntryPoint()
	{
		BasicAuthenticationEntryPoint basicAuthenticationEntryPoint = new BasicAuthenticationEntryPoint();
		basicAuthenticationEntryPoint.setRealmName("RestFUL Realm");
		return basicAuthenticationEntryPoint;
	}
	
	@Bean
	protected LoginUrlAuthenticationEntryPoint getLoginUrlAuthenticationEntryPoint()
	{
		return new LoginUrlAuthenticationEntryPoint("/login");
	}
	
	@Bean
	@Override
	public AuthenticationManager authenticationManagerBean() throws Exception {
		AuthenticationManager authenticationManager = super.authenticationManagerBean();
		return authenticationManager;
	}
	/*@Bean
	protected AuthenticationProvider getAuthenticationProvider()
	{
		;
	}*/
}
